package com.ntt.benew.dictionary.view

import android.animation.AnimatorInflater
import android.content.Context
import android.graphics.Typeface
import android.util.AttributeSet
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayout
import com.ntt.benew.dictionary.R
import com.ntt.benew.dictionary.entity.gpt.ChunkingEntity
import com.ntt.benew.dictionary.entity.gpt.CoreWordEntity
import com.ntt.benew.dictionary.entity.gpt.WordCoreEntity
import com.ntt.benew.dictionary.listener.IClickPlayerListener
import com.ntt.benew.dictionary.listener.TxtOnClickListener
import com.ntt.core.nlogger.NLogger

/**
 * Created by Android Studio.
 * User: tw
 * Date: 2023/11/6
 * Time: 11:55
 * 句子的容器
 */
class SentenceFlexView(context: Context, attrs: AttributeSet) : FlexboxLayout(context, attrs) {

    init {
        flexDirection = FlexDirection.ROW
        flexWrap = FlexWrap.WRAP

    }

    var mTxtOnClickListener: TxtOnClickListener? = null

    /**
     * 点击播放 监听
     */
    var mOnClickPlayListener: IClickPlayerListener? = null

    /**
     * 是否要隐藏喇叭
     */
    private var isHideHorn: Boolean = false

    /**
     * 初始状态 显示原文句子
     */
    fun setTxt(sentence: String) {
        removeAllViews()
        val txts = sentence.split(" ")
        txts.forEach {
            addView(
                TextView(context).apply {
                    // 由于前面拆句将空格拆掉了 这里补一个空格
                    text = "$it "
                    textSize = 29f
                    typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                    setTextColor(ContextCompat.getColor(context, R.color.ntt_000000))
                }
            )
        }
        renderEndImage(sentence)
    }

    /**
     * 隐藏喇叭
     */
    fun hideHorn() {
        isHideHorn = true
        for (i in 0 until childCount) {
            getChildAt(i).takeIf { it is ImageView }?.let { removeView(it) }
        }
    }

    /**
     * 设置数据
     * @param major 核心单词数据
     * @param sentencePart 句子分析后的数据
     * @param original 原句
     */
    fun setData(
        major: List<CoreWordEntity>?,
        sentencePart: List<ChunkingEntity>?,
        original: String
    ) {
        val coreMap: MutableMap<String, String?> = mutableMapOf()
        major?.forEach {
            it.word?.let { word ->
                coreMap[word] = it.translate
            }
        }
        NLogger.d("核心单词：$coreMap")
        if (sentencePart == null) {
            return
        }
        removeAllViews()
        sentencePart.forEach {
            val txts = it.text?.split(" ")
            val sentences: MutableList<WordCoreEntity> = mutableListOf()
            txts?.forEach { txt ->
                val wordEntity: WordCoreEntity = if (coreMap.containsKey(txt)) {
                    WordCoreEntity().apply {
                        word = txt
                        isCore = true
                        translation = coreMap[txt]
                    }
                } else {
                    WordCoreEntity().apply {
                        word = txt
                        isCore = false
                    }
                }
                sentences.add(wordEntity)
            }
            renderSentence(it, sentences)
        }
        renderEndImage(original)
    }

    /**
     * 开始绘制句子
     */
    private fun renderSentence(
        chunkingEntity: ChunkingEntity,
        sentenceList: MutableList<WordCoreEntity>
    ) {
        val wordCallView = WordCallView(context)
        wordCallView.mTextClickListener = mTxtOnClickListener
        wordCallView.setData(chunkingEntity, sentenceList)
        addView(wordCallView)
    }

    /**
     * 画结束处的小喇叭
     */
    private fun renderEndImage(sentence: String) {
        if (isHideHorn){
            return
        }
        val imgView = ImageView(context)
        imgView.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_horn))
        imgView.layoutParams =
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
                alignSelf = AlignItems.FLEX_END
            }
        imgView.stateListAnimator = AnimatorInflater.loadStateListAnimator(context, R.drawable.btn_press)
        imgView.tag = sentence
        imgView.setOnClickListener {
            mOnClickPlayListener?.play(it.tag as String)
        }
        addView(imgView)
    }


}